package com.src.iseclive.base;

import android.os.Bundle;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.src.iseclive.integration.lifecycle.FragmentLifecycleable;
import com.src.iseclive.mvp.presenter.IPresenter;
import com.src.iseclive.mvp.view.IView;
import com.trello.rxlifecycle2.android.FragmentEvent;

import javax.inject.Inject;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import io.reactivex.subjects.BehaviorSubject;
import io.reactivex.subjects.Subject;
import timber.log.Timber;

/**
 * @author liujiancheng
 * @name IsecLive
 * @class name：com.src.iseclive.base
 * @class Fragment基类
 * 完成presenter的注入、butterknife的绑定以及RxLifecycle的生命周期绑定
 * @time 2018/3/19 16:01
 * @change
 * @chang time
 * @class describe
 */

public abstract class BaseFragment<P extends IPresenter> extends Fragment implements
        FragmentLifecycleable {

    protected final String TAG = this.getClass().getSimpleName();
    private final BehaviorSubject<FragmentEvent> mLifecycleSubject = BehaviorSubject.create();
    private Unbinder mUnbinder;

    @Inject
    @Nullable
    protected P mPresenter;//如果当前页面逻辑简单, Presenter 可以为 null

    @NonNull
    @Override
    public Subject<FragmentEvent> provideLifecycleSubject() {
        return mLifecycleSubject;
    }


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        return initView(inflater, container, savedInstanceState);
    }


    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        //绑定butterknife
        if (view != null)
            mUnbinder = ButterKnife.bind(this, view);
        //将Presenter关联View
        if (mPresenter != null && this instanceof IView)
            mPresenter.attachView((IView) this);

    }


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initialize(savedInstanceState);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (mUnbinder != null && mUnbinder != Unbinder.EMPTY) {
            try {
                mUnbinder.unbind();
            } catch (IllegalStateException e) {
                e.printStackTrace();
                //fix Bindings already cleared
                Timber.w(TAG + ":onDestroyView: " + e.getMessage());
            }
        }
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        //释放资源
        if (mPresenter != null)
            mPresenter.onDestroy();
        this.mPresenter = null;
        this.mUnbinder = null;
    }


    /**
     * @author liujiancheng
     * @time 2018/3/21  9:44
     * @describe 初始化 View
     */
    protected abstract View initView(@NonNull LayoutInflater inflater, @Nullable ViewGroup
            container, @Nullable Bundle savedInstanceState);

    /**
     * @author liujiancheng
     * @time 2018/3/21  9:36
     * @describe 初始化数据/开始
     * 等同于onActivityCreated()
     */
    protected abstract void initialize(@Nullable Bundle savedInstanceState);


    /**
     * @author liujiancheng
     * @time 2018/3/21  9:45
     * @describe Fragment与寄生的容器进行通信的函数
     * 如 Activity 想让自己持有的某个 Fragment 对象执行一些方法,就重写此方法
     * 统一传 {@link Message}, 通过 what 字段来区分不同的方法, 在 {@link #communicationFragment(Message)}
     * 方法中就可以 {@code switch} 做不同的操作, 这样就可以用统一的入口方法做多个不同的操作, 可以起到分发的作用
     * 使用时注意Fragment的生命周期,如果用到Presenter注意Presenter的注入时间，避免空指针
     */
    protected void communicationFragment(Message message) {

    }

}
